<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="知识库标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入知识库标题" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="录入时间">
        <el-date-picker v-model="daterangeTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="录入人" prop="inputer">
        <el-input v-model="queryParams.inputer" placeholder="请输入录入人" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['dr:knowledge:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['dr:knowledge:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['dr:knowledge:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['dr:knowledge:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="knowledgeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="知识库标题" align="center" prop="title" />
      <el-table-column label="知识库图片" align="center" prop="imagepath" width="200">
        <template slot-scope="scope">
          <el-image style="width: 100%; height: 100px" :src="baseURL+scope.row.imagepath"></el-image>
        </template>
      </el-table-column>
       <el-table-column label="录入人" align="center" prop="inputer" />
      <el-table-column label="知识库内容" align="left" prop="content" width="500">
        <template slot-scope="scope">
          <span>   {{scope.row.content.substring(0,90)}}<b>.....</b>
            <el-popover placement="top-start" title="标题" width="600" trigger="hover">
              <div>{{scope.row.content}}</div>
              <el-tag slot="reference" size="medium">详情</el-tag>
            </el-popover>

          </span>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['dr:knowledge:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['dr:knowledge:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改地热知识库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="知识库图片" prop="imagepath">
          <el-progress :text-inside="true" :stroke-width="20" :percentage="percentage"></el-progress>
          <el-upload ref="upload" :limit="1" :before-upload="getFileType" :accept="upload.file_type" :action="upload.url" :data="upload.data" :headers="upload.headers" :show-file-list="false" :file-list="upload.fileList" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false">
            <el-button slot="trigger" size="mini" round type="primary" icon="el-icon-folder-opened" @click="handleAdd">选取文件</el-button>
            <el-button style="margin-left: 10px;" round size="mini" icon="el-icon-upload" type="success" :loading="upload.isUploading" @click="submitUpload">上传到服务器</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="知识库标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入知识库标题" />
        </el-form-item>
        <el-form-item label="知识库内容">
          <el-input v-model="form.content" :min-height="192" type="textarea" autosize />
        </el-form-item>
        <!-- <el-form-item label="录入时间" prop="time">
          <el-date-picker clearable size="small" v-model="form.time" type="date" value-format="yyyy-MM-dd" placeholder="选择录入时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="录入人" prop="inputer">
          <el-input v-model="form.inputer" placeholder="请输入录入人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKnowledge, getKnowledge, delKnowledge, addKnowledge, updateKnowledge } from "@/api/warning/geo_knowledge";
import { getToken } from "@/utils/auth";
export default {
  name: "Knowledge",
  data() {
    return {
      baseURL: '/dev-api',
      percentage: 0,
      // 上传参数
      upload: {
        file_type: ".jpg,.jpeg,.png,.JPG,.JPEG",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/dr/upload/morefile",
        // 上传的文件列表
        fileList: [],
        // projectno:  drfb 41862   kfly 1161225  gzgx 726724 drcy 418325   file_module drfbtu shuizhitu zhuzhuangtu
        data: { projectno: 11141523, file_type: '.jpg, .png', uploader: null, file_module: "drknowledges" }
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地热知识库表格数据
      knowledgeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 录入人时间范围
      daterangeTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        time: null,
        inputer: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.baseURL = process.env.VUE_APP_BASE_API;
    console.log(this.baseURL)
    this.getList();

    console.log(this.form.inputer)
  },
  methods: {
    /**上传 */
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      console.log(event)
      console.log(file)
      this.upload.isUploading = true;
      this.percentage = event.loaded / event.total * 100 | 0;  //百分比
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();

      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.form.imagepath = response.fileName;
      console.log(response)
    },
    handleAdd() {
      this.percentage = 0;
      this.upload.fileList = [];
    },
    handleUpdate(row) {

      this.upload.fileList = [{ name: this.form.fileName, url: this.form.filePath }];
    },
    // 文件提交处理
    submitUpload() {
      this.$refs.upload.submit();
    },
    getFileType(file) {
      const fileName = file.name;
      const fileType = fileName.substring(fileName.lastIndexOf('.'));
      this.upload.data.file_type = fileType;
    },
    /**上传结束 */
    /** 查询地热知识库列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      listKnowledge(this.queryParams).then(response => {
        this.knowledgeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        imagepath: null,
        content: null,
        time: null,
        inputer: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地热知识库";
      this.form.inputer = this.$store.getters.name;
      this.percentage = 0;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKnowledge(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地热知识库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKnowledge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.time = new Date().format("yyyy-MM-dd hh:mm");
            addKnowledge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除地热知识库编号为"' + ids + '"的数据项？').then(function () {
        return delKnowledge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dr/knowledge/export', {
        ...this.queryParams
      }, `knowledge_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
